package toanstt.sprites;

import android.graphics.Canvas;

public class Sprite 
{
	Sprites sprites;
	int currentAnimation;
	boolean canLoop;
	boolean ShowLastFrame;
	int waitDelay;
	int currentFrame = 0;
	int xpos=0;
	int ypos=0;
	float temp;
	int width_basic;
	int height_basic;
	int newwidth;
	int frameIndex;
	public boolean isdisable=false;
	public Sprite(Sprites _sprites,int id,boolean loop,boolean last)
	{
		sprites = _sprites;
		currentAnimation = id;
		canLoop = loop;
		ShowLastFrame = last;
		waitDelay =0;
		width_basic = sprites.GetWidth(currentAnimation);
		height_basic = sprites.GetHeight(currentAnimation);
	}
	public void ReNew()
	{
		waitDelay =0;
		currentFrame = 0;
		isdisable=false;
	}
	public Sprite(Sprites _sprites,int id,int x,int y,boolean loop,boolean last)
	{
		
		sprites = _sprites;
		currentAnimation = id;
		canLoop = loop;
		ShowLastFrame = last;
		waitDelay =0;
		xpos =x;
		ypos =y;
		width_basic = sprites.GetWidth(currentAnimation);
		height_basic = sprites.GetHeight(currentAnimation);
	}
	public void SetPos(int x,int y)
	{
		xpos =x;
		ypos =y;
	}
	public void SetPosMove(int dx,int dy)
	{
		xpos +=dx;
		ypos +=dy;
	}
	
	public void Draw(Canvas g, int angle,int Scalex,int Scaley, int x, int y)
	{
		if (currentFrame < sprites.anims[currentAnimation].numOfFrame)
			sprites.drawAFrame(g, sprites.anims[currentAnimation].arrayFrameIndex[currentFrame],angle,Scalex,Scaley, x
					+ sprites.anims[currentAnimation].postionX[currentFrame], y
					+ sprites.anims[currentAnimation].postionY[currentFrame]);
		Update();
	}
	public void Draw(Canvas g,float Scale)
	{
		if (currentFrame < sprites.anims[currentAnimation].numOfFrame)
			sprites.drawAFrame(g, sprites.anims[currentAnimation].arrayFrameIndex[currentFrame],0,Scale,Scale, xpos
					+ sprites.anims[currentAnimation].postionX[currentFrame], ypos
					+ sprites.anims[currentAnimation].postionY[currentFrame]);
		Update();
	}
	public void Draw(Canvas g,int newheight)
	{
		if(isdisable == true)
			return;
		temp = newheight*1.0f/width_basic;
		
		if (currentFrame < sprites.anims[currentAnimation].numOfFrame)
		{
			sprites.drawAFrame(g, sprites.anims[currentAnimation].arrayFrameIndex[currentFrame],0,temp,temp, xpos
					+ sprites.anims[currentAnimation].postionX[currentFrame], ypos
					+ sprites.anims[currentAnimation].postionY[currentFrame]);
		Update();
		}
		else if (ShowLastFrame)
		{
			frameIndex = sprites.anims[currentAnimation].numOfFrame - 1;
			sprites.drawAFrame(g, sprites.anims[currentAnimation].arrayFrameIndex[frameIndex],0,temp,temp, xpos
					+ sprites.anims[currentAnimation].postionX[frameIndex], ypos
					+ sprites.anims[currentAnimation].postionY[frameIndex]);
		}
		else
			isdisable=true;
	}
	public void Draw(Canvas g,int newheight,boolean isaligncenter)
	{
		if(isdisable == true)
			return;
		
		temp = newheight*1.0f/width_basic;
		newwidth = (int)(width_basic*temp);
		if (currentFrame < sprites.anims[currentAnimation].numOfFrame)
		{
			sprites.drawAFrame(g, sprites.anims[currentAnimation].arrayFrameIndex[currentFrame],0,temp,temp, xpos -newwidth/2
					+ sprites.anims[currentAnimation].postionX[currentFrame], ypos-newheight/2
					+ sprites.anims[currentAnimation].postionY[currentFrame]);
		Update();
		}
		else if (ShowLastFrame)
		{
			frameIndex = sprites.anims[currentAnimation].numOfFrame - 1;
			sprites.drawAFrame(g, sprites.anims[currentAnimation].arrayFrameIndex[frameIndex],0,temp,temp, xpos -newwidth/2
					+ sprites.anims[currentAnimation].postionX[frameIndex], ypos-newheight/2
					+ sprites.anims[currentAnimation].postionY[frameIndex]);
		}
		else
			isdisable=true;
		
	}
	public void Draw(Canvas g) 
	{
		if (currentFrame < sprites.anims[currentAnimation].numOfFrame)
			sprites.drawAFrame(g, sprites.anims[currentAnimation].arrayFrameIndex[currentFrame],
					xpos + sprites.anims[currentAnimation].postionX[currentFrame], ypos
							+ sprites.anims[currentAnimation].postionY[currentFrame]);
		else if (ShowLastFrame) 
		{
			int frameIndex = sprites.anims[currentAnimation].numOfFrame - 1;
			sprites.drawAFrame(g, sprites.anims[currentAnimation].arrayFrameIndex[frameIndex],
					xpos + sprites.anims[currentAnimation].postionX[frameIndex], ypos
							+ sprites.anims[currentAnimation].postionY[frameIndex]);
		}

		Update();
	}
	public void Update()
	{
		waitDelay++;
		if (currentFrame <= sprites.anims[currentAnimation].numOfFrame - 1) 
		{
			if (waitDelay > sprites.anims[currentAnimation].timeDelay[currentFrame]) {
				currentFrame++;
				waitDelay = 0;
			}
		}
		if (currentFrame >= sprites.anims[currentAnimation].numOfFrame) {
			if (canLoop) {
				currentFrame = 0;
				waitDelay = 0;
			} else if (ShowLastFrame) {
				//	currentFrame = anims[currentAnimation].animFramesLen - 1;
			}
		}
	}
	
}
